package thread_pool;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IDEA
 * author:ju bai
 * Date:2021/5/23
 * Time:10:05
 * 其他3种拒绝策略
 **/
public class PoolDemo15 {
    public static void main(String[] args) throws InterruptedException {
        //创建线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                5, 5, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(5),
                new ThreadPoolExecutor.CallerRunsPolicy())
                //new ThreadPoolExecutor.DiscardPolicy()//忽略新任务（）
                //new ThreadPoolExecutor.DiscardOldestPolicy()//忽略老任务，（最先加入任务队列的任务）
                ;


        for (int i = 0; i < 11; i++) {
            int finalI = i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("任务："+finalI+
                            "线程名称："+Thread.currentThread().getName());
                }
            });

            //休眠后，使用空闲线程来执行任务，不会实现拒绝策略
            //Thread.sleep(200);
        }

    }

}
